package com.bt.framework.ext

import android.content.Context
import android.content.res.Resources
import android.icu.text.DecimalFormat
import android.view.View
import android.view.ViewGroup
import androidx.annotation.ArrayRes
import androidx.annotation.ColorRes
import androidx.annotation.DimenRes
import androidx.annotation.DrawableRes
import androidx.annotation.IntegerRes
import androidx.annotation.LayoutRes
import androidx.annotation.StringRes
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.recyclerview.widget.RecyclerView
import com.bt.framework.helper.SumAppHelper
import com.bt.framework.manager.AppManager
import java.math.BigDecimal

/**
 * 获取颜色
 */
fun Context.color(@ColorRes colorRes: Int) = ContextCompat.getColor(this, colorRes)

/**
 * 获取字符串
 */
fun Context.string(@StringRes strRes: Int) = resources.getString(strRes)

/**
 * 获取字符串数组
 */
fun Context.stringArray(@ArrayRes arrayRes: Int) = resources.getStringArray(arrayRes)

/**
 * 获取图片
 */
fun Context.drawable(@DrawableRes drawableRes: Int) = ContextCompat.getDrawable(this, drawableRes)

/**
 * 获取PX尺寸
 */
fun Context.dimenPx(@DimenRes dimenRes: Int) = resources.getDimensionPixelSize(dimenRes)

/**
 * 获取整型
 */
fun Context.integer(@IntegerRes integerRes: Int) = resources.getInteger(integerRes)

/**
 * 获取颜色
 */
fun View.color(@ColorRes colorRes: Int) = context.color(colorRes)

/**
 * 获取字符串
 */
fun View.string(@StringRes strRes: Int) = context.string(strRes)

/**
 * 获取字符串数组
 */
fun View.stringArray(@ArrayRes arrayRes: Int) = context.stringArray(arrayRes)

/**
 * 获取图片
 */
fun View.drawable(@DrawableRes drawableRes: Int) = context.drawable(drawableRes)

/**
 * 获取PX尺寸
 */
fun View.dimenPx(@DimenRes dimenRes: Int) = context.dimenPx(dimenRes)

/**
 * 获取整型
 */
fun View.integer(@IntegerRes integerRes: Int) = context.integer(integerRes)

/**
 * 获取颜色
 */
fun Fragment.color(@ColorRes colorRes: Int) = requireContext().color(colorRes)

/**
 * 获取字符串
 */
fun Fragment.string(@StringRes strRes: Int) = requireContext().string(strRes)

/**
 * 获取字符串数组
 */
fun Fragment.stringArray(@ArrayRes arrayRes: Int) = requireContext().stringArray(arrayRes)

/**
 * 获取图片
 */
fun Fragment.drawable(@DrawableRes drawableRes: Int) = requireContext().drawable(drawableRes)

/**
 * 获取PX尺寸
 */
fun Fragment.dimenPx(@DimenRes dimenRes: Int) = requireContext().dimenPx(dimenRes)

/**
 * 获取整型
 */
fun Fragment.integer(@IntegerRes integerRes: Int) = requireContext().integer(integerRes)

/**
 * 获取颜色
 */
fun RecyclerView.ViewHolder.color(@ColorRes colorRes: Int) = itemView.color(colorRes)

/**
 * 获取字符串
 */
fun RecyclerView.ViewHolder.string(@StringRes strRes: Int) = itemView.string(strRes)

/**
 * 获取字符串数组
 */
fun RecyclerView.ViewHolder.stringArray(@ArrayRes arrayRes: Int) = itemView.stringArray(arrayRes)

/**
 * 获取图片
 */
fun RecyclerView.ViewHolder.drawable(@DrawableRes drawableRes: Int) = itemView.drawable(drawableRes)

/**
 * 获取PX尺寸
 */
fun RecyclerView.ViewHolder.dimenPx(@DimenRes dimenRes: Int) = itemView.dimenPx(dimenRes)

/**
 * 获取整型
 */
fun RecyclerView.integer(@IntegerRes integerRes: Int) = context.integer(integerRes)

/**
 * 获取LayoutInflater
 */
inline val Context.layoutInflater: android.view.LayoutInflater get() = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as android.view.LayoutInflater

fun inflateLayout(
    @LayoutRes layoutId: Int,
    parent: ViewGroup? = null,
    attachToParent: Boolean = false
) : View{
    return context.layoutInflater.inflate(layoutId, parent, attachToParent)
}

/**
 * dp转px
 */
fun dp2Px(dpValue: Float) = (dpValue * res.displayMetrics.density + 0.5f).toInt()

/**
 * px转dp
 */
fun px2Dp(pxValue: Float) = (pxValue / res.displayMetrics.scaledDensity + 0.5f).toInt()

/**
 * sp转px
 */
fun sp2Px(spValue: Float) = (spValue * res.displayMetrics.scaledDensity + 0.5f).toInt()

/**
 * px转sp
 */
fun px2Sp(pxValue: Float) = (pxValue / res.displayMetrics.scaledDensity + 0.5f).toInt()

/**
 * DIP转PX
 */
fun Number.dp():Int = dp2Px(this.toFloat())

/**
 * SP转PX
 */
fun Number.sp():Int = sp2Px(this.toFloat())

/**
 * 屏幕宽度
 */
val screenWidth : Int
    get() = AppManager.getScreenWidthPx()

/**
 * 屏幕高度
 */
val screenHeight : Int
    get() = AppManager.getScreenHeightPx()

private val res : Resources
    get() = context.resources

private val context : Context
    get() = SumAppHelper.getApplication()

/**
 * 获取Activity
 */
inline val Context.asActivity
    get() = this as FragmentActivity

//数字转百分比 ，0.50->50%
inline val String.formatWithPercent: String
    get() = run {
        return kotlin.runCatching {
            BigDecimal(this).multiply(BigDecimal("100")).stripTrailingZeros()
                .toPlainString() + "%"
        }.getOrDefault(this)
    }

//数字格式化，100000->100,000
inline val String.formatWithComma: String
    get() = run {
        return runCatching {
            val df = DecimalFormat()
            df.applyPattern(",###.########")
            df.format(toDouble())
        }.getOrDefault(this)
    }